//
//  CJYearMonthSelectedView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/9.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "CJYearMonthSelectedView.h"
@interface CJYearMonthSelectedView () <UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *picker;    //选择器
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *minDateStr;
@property (strong, nonatomic) BRDateResultBlock resultBlock;
@property (copy, nonatomic) NSString *selectValue;   //选择的值
@property (strong, nonatomic) NSMutableArray<NSString *> *data;
@property (assign, nonatomic) BOOL showDay;//是否显示天

@end
@implementation CJYearMonthSelectedView
+ (void)showDatePickerWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock{
    CJYearMonthSelectedView *datePicker = [[CJYearMonthSelectedView alloc] initWithTitle:title minDateStr:minDateStr resultBlock:resultBlock];
    [datePicker showWithAnimation:YES];
}
+ (void)showDatePickerWithTitle:(NSString *)title andTypeOfFrame:(CGRect)rect minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock{
    CJYearMonthSelectedView *datePicker = [[CJYearMonthSelectedView alloc]initWithTitle:title minDateStr:minDateStr resultBlock:resultBlock];
    datePicker.mainRect = rect;
    [datePicker reloadViewFrame:rect];
    [datePicker showAlertViewWithFrame:rect];
}

- (instancetype)initWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock{
    if (self = [super init]) {
        _title = title;
        _minDateStr = minDateStr;
        _resultBlock = resultBlock;
        [self initUI];
    }
 return self;
}
//UI布局，主要就是在弹出视图上添加选择器
- (void)initUI{
    [super initUI];
    self.titleLabel.text = _title;
    // 添加时间选择器
    [self.alertView addSubview:self.picker];
  }
//选择器的初始化和布局
- (UIPickerView *)picker{
    if (!_picker) {
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,50, SCREEN_WIDTH, kDatePicHeight)];
        //        _picker.backgroundColor = [UIColor whiteColor];
        _picker.showsSelectionIndicator = YES;
        //设置代理
        _picker.delegate =self;
        _picker.dataSource =self;
    }
    return _picker;
}

//选择器数据的加载，从设定的最小日期到当前月
- (NSMutableArray<NSString *> *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
        [_data addObject:@"全部"];
        NSDate *currentDate = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM"];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        NSString *dateStr = [formatter stringFromDate:currentDate];
        NSInteger lastIndex = 0;
        NSDate *newdate;
        //循环获取可选月份，从当前月份到最小月份
        while (!([dateStr compare:self.minDateStr] == NSOrderedAscending)) {
            [_data addObject:dateStr];
            lastIndex--;
            //获取之前几个月
            [lastMonthComps setMonth:lastIndex];
            newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
            dateStr = [formatter stringFromDate:newdate];
            }
        self.selectValue = [self.data firstObject];
       }
//    NSLog(@"时间 %@",_data);
   return _data;
}

#pragma mark - UIPickerView的数据和布局，和tableview类似
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回多少行
 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.data.count;
}

//每一行的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.data[row];
}

//选中时的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//    if (component == 0) {
//        self.selectValue = @"0";
//    } else {
        self.selectValue = self.data[row];
//    }
    
}

//返回高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35.0f;
}

//返回宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 300*fw;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    //    [self dismissWithAnimation:NO];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    CGRect rect = self.alertView.frame;
    rect.origin.y = 64;
    self.alertView.frame = rect;
}
- (void)showAlertViewWithFrame:(CGRect )rect{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
//    NSLog(@"frame大大的 %f  %f",rect.origin.y,rect.origin.x);
//    CGRect rect1 = self.alertView.frame;
//    rect1.origin.y = 330;
//    self.alertView.frame = rect1;
}
#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
        [self.leftBtn removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.picker removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];

}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
}

#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn {
    NSLog(@"点击确定按钮后，执行block回调");
    [self dismissWithAnimation:YES];
    if (_resultBlock) {
        _resultBlock(_selectValue);
    }
}

@end
